package android.slc.fast.vd.list

import android.slc.box.core.property.DataChangeListener
import android.slc.adapter.ISelectBox
import android.slc.adapter.PageModel
import javafx.scene.control.CheckBox
import javafx.scene.control.Pagination
import javafx.scene.control.TableView
import java.io.Serializable

open class BaseListPageViewVdByFx<T : Serializable, VS : BaseListPageVmSubByFx<T>> : BaseListPageVdByFx<T, VS>() {

    protected var cbTableSelect: CheckBox? = null

    protected lateinit var tableView: TableView<T>

    protected lateinit var pagination: Pagination

    protected val paginationViewHelper = PaginationViewHelper()

    override fun initView() {
        super.initView()
        cbTableSelect = contentView.lookup(getTableSelectId()) as CheckBox?
        cbTableSelect?.selectedProperty()?.addListener(DataChangeListener { isSelected ->
            onTableSelect()
        })
        paginationViewHelper.initPagination(contentView,vmSub)
    }

    protected open fun onTableSelect() {
        val itemList = mutableListOf<T>()
        tableView.items.forEach { itItem ->
            if (itItem is ISelectBox<*>) {
                itItem.isChecked = cbTableSelect?.isSelected ?: false
            }
            itemList.add(itItem)
        }
        tableView.items.setAll(itemList)
    }

    override fun initSwipeRecycler() {
        super.initSwipeRecycler()
        this.tableView = this.contentView.lookup(getTableViewId()) as TableView<T>
    }

    protected open fun getTableSelectId(): String {
        return "#cbTableSelect"
    }

    protected open fun getTableViewId(): String {
        return "#tableView"
    }

    override fun loadMoreComplete(pageModel: PageModel<T>) {
        paginationViewHelper.loadMoreComplete(pageModel)
        tableView.items.setAll(pageModel.listNoNull)
        super.loadMoreComplete(pageModel)
    }

}
